const { sequelize } = require("../db/index")
const { DataTypes, Model } = require("sequelize")
const User = require("./user_model")
const Cleaner = require("./cleaner_model")
const moment = require("moment")

class GoodsAttrValue extends Model {
  static async defaultValue (data) {
    const objValue = {
      // id: data?.id || "",
      goods_id: data?.goods_id || "",
      goods_name: data?.goods_name || "",
      sku: data?.sku || "",
      store_house_id: data?.store_house_id || "",
      stock: data?.stock || 0,
      sales: data?.sales || "",
      price: data?.price || "",
      cost_price: data?.cost_price || "",
      weight: data?.weight || "",
      volume: data?.volume || "",
      image: data?.image || "",
      sort: data?.sort || "",
      update_time: data?.update_time || ""
    }
    return objValue
  }
  // static async hasGoodsAttr (id, goods_id) {
  //   const attr = await GoodsAttr.findOne({ where: { id, goods_id } })
  static async hasGoodsAttr (goods_id) {
    const attr = await GoodsAttr.findOne({ where: { goods_id } })
    if (attr === null) {
      return false
    } else {
      return true
    }
  }
  static async createGoodsAttr ({
    goods_id,
    store_id,
    attr_name,
    attr_values,
    is_delete
  }) {
    const data = await GoodsAttr.create({
      goods_id,
      store_id,
      attr_name,
      attr_values,
      is_delete
    })
    return data
  }
  static async addAdmin (buildingId, userId) {
    try {
      const cate = await Cate.findOne({ where: { id: buildingId } })
      const user = await User.findOne({ where: { id: userId } })
      const result = await cate.addAdmin(user)
      return result
    } catch (e) {
      throw new Error("已存在该管理员，添加失败")
    }
  }
  static async addCleaner (buildingId, cleanerId) {
    try {
      const cleaner = await Cleaner.findOne({ where: { id: cleanerId } })
      cleaner.buildingId = buildingId
      return await cleaner.save()
    } catch (e) {
      throw new Error("添加保洁员失败")
    }
  }
}

GoodsAttrValue.init(
  {
    id: {
      comment: "主键ID",
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
      allowNull: false
    },
    goods_id: {
      comment: "商品ID",
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: false,
      allowNull: false
    },
    goods_name: {
      comment: "商品名称",
      type: DataTypes.STRING,
      autoIncrement: false,
      allowNull: false
    },
    sku: {
      comment: "商品属性",
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: false,
      allowNull: true
    },
    store_house_id: {
      comment: "仓库ID",
      type: DataTypes.INTEGER,
      autoIncrement: false,
      allowNull: false
    },
    stock: {
      comment: "库存",
      type: DataTypes.INTEGER,
      autoIncrement: false,
      allowNull: false
    },
    cordon: {
      comment: "库存警戒线",
      type: DataTypes.INTEGER,
      autoIncrement: false,
      allowNull: false
    },
    sales: {
      comment: "销量",
      type: DataTypes.INTEGER,
      autoIncrement: false,
      allowNull: false
    },
    price: {
      comment: "价格",
      type: DataTypes.INTEGER,
      autoIncrement: false,
      allowNull: false
    },
    cost_price: {
      comment: "成本价格",
      type: DataTypes.INTEGER,
      autoIncrement: false,
      allowNull: false
    },
    weight: {
      comment: "重量",
      type: DataTypes.INTEGER,
      autoIncrement: false,
      allowNull: false
    },
    volume: {
      comment: "体积",
      type: DataTypes.INTEGER,
      autoIncrement: false,
      allowNull: false
    },
    image: {
      comment: "图片",
      type: DataTypes.STRING,
      autoIncrement: false,
      allowNull: false
    },
    sort: {
      comment: "图片",
      type: DataTypes.INTEGER,
      autoIncrement: false,
      allowNull: false
    },
    update_time: {
      comment: "更新时间",
      type: DataTypes.STRING,
      get () {
        return moment(this.getDataValue("update_time")).format(
          "YYYY-MM-DD HH:mm:ss"
        )
      }
    }
  },
  {
    sequelize,
    modelName: "sp_store_goods_attr_value",
    freezeTableName: true,
    paranoid: false,
    timestamps: false
  }
)
// GoodsAttrValue.removeAttribute("id")

module.exports = GoodsAttrValue
